<?php 
require_once('../../config.php');
require_once($CFG->dirroot . '/blocks/myguestbook/lib.php');
global $DB, $OUTPUT, $PAGE, $CFG, $COURSE, $USER;

require_once($CFG->dirroot . '/blocks/myguestbook/lib.php');
require_once($CFG->dirroot . '/lib/outputcomponents.php');
require_once($CFG->libdir . '/outputcomponents.php');
require_once($CFG->dirroot . '/blocks/myguestbook/block_myguestbook_form.php');
require_once($CFG->dirroot . '/blocks/myguestbook/block_myguestbook_editable_form.php');

require_login();

$PAGE->requires->css(new moodle_url('/blocks/myguestbook/style/myguestbook.css'));

$PAGE->set_context(context_course::instance($COURSE->id));
$PAGE->set_url('/blocks/myguestbook/view.php');

$PAGE->set_heading(get_string('pluginname', 'block_myguestbook'));
$PAGE->set_title(get_string('pluginname', 'block_myguestbook'));

$mform = new block_myguestbook_form();
$eform = new block_myguestbook_editable_form();

$PAGE->navbar->ignore_active();

// навигация
$PAGE->navbar->add(get_string(
                'pluginname', 'block_myguestbook'), new moodle_url('/blocks/myguestbook/view.php'));

$previewnode = $PAGE->navigation->add(get_string(
                'pluginname', 'block_myguestbook'), new moodle_url('/blocks/myguestbook/view.php'),
        navigation_node::TYPE_CONTAINER
);

$main_url = new moodle_url("/blocks/myguestbook/view.php");

$data = get_messages();

if (($fromform = $mform->get_data())) {
    $mform->set_data(" ");
    set_message($fromform->name, $fromform->message);
    redirect($main_url);
} else if (($messageid = optional_param('messageid', null, PARAM_INT))) {
    delete_message($messageid);
    redirect($main_url);
} else if (($commentid = optional_param('commentid', null, PARAM_INT))) {
    delete_comment($commentid);
    redirect($main_url);
}

echo $OUTPUT->header();

echo $OUTPUT->container_start('form_wrap');
$mform->display();
echo $OUTPUT->container_end();


foreach ($data as $num => $message) {
    
    $data_c = get_comments($message->id);
    
    $button_url = new moodle_url("/blocks/myguestbook/view.php", 
            array('messageid' => $message->id, 'param' => 'message'));

    $edit_url = new moodle_url('/blocks/myguestbook/edit.php', 
            array('edparam' => 'Edit', 'messageid' => $message->id));

    $comment_url = new moodle_url("/blocks/myguestbook/edit.php",
            array('edparam' => 'Comment', 'messageid' => $message->id));

    echo $OUTPUT->container_start('wrap_post');
        echo $OUTPUT->notification($message->name, 'name');
        echo $OUTPUT->single_button($button_url, get_string('delete', 'block_myguestbook'));
        echo $OUTPUT->notification(date("H:i:s", $message->time), 'time');
        echo $OUTPUT->notification($message->message, 'message');
        echo $OUTPUT->single_button($edit_url, get_string('edit', 'block_myguestbook'));
    echo $OUTPUT->container_end();

    echo $OUTPUT->single_button($comment_url, get_string('comment', 'block_myguestbook'));

    foreach ($data_c as $num => $comment) {
        if ($comment->messageid === $message->id) {

            $button_url = new moodle_url("/blocks/myguestbook/view.php", 
                    array('commentid' => $comment->id, 'param' => 'comment'));

            echo $OUTPUT->container_start('wrap_comment');
                echo $OUTPUT->notification($comment->name, 'name');
                echo $OUTPUT->single_button($button_url, get_string('delete', 'block_myguestbook'));
                echo $OUTPUT->notification(date("H:i:s", $comment->time), 'time');
                echo $OUTPUT->container($comment->comment, 'message');
            echo $OUTPUT->container_end();
        }
    }
}

echo $OUTPUT->footer();